package org.yxing.palpalgps.oauth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.yxing.palpalgps.domain.UserInfo;
import org.yxing.palpalgps.utils.ConfigHelper;
import org.yxing.palpalgps.utils.Helper;
import org.yxing.palpalgps.utils.Utils;

import android.util.Log;

public class OAuth {
    
    /**
     * @deprecated
     * @param url
     * @param params
     * @return
     */
    public HttpResponse signRequest(String url, List<BasicNameValuePair> params) {
        
        Log.v("request", url);
        HttpPost post = new HttpPost(url);
        HttpResponse resp = null;
        
        if (params == null)
        	params = getAuthParams(null);
        
        Log.v("http_post_data", params.toString());

        try {
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            resp = new DefaultHttpClient().execute(post);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return resp;
    }
    
    /**
     * @deprecated
     * @param params
     * @return
     */
    public ArrayList<BasicNameValuePair> getAuthParams(ArrayList<BasicNameValuePair> params) {
    	if (params == null) 
            params = new ArrayList<BasicNameValuePair>();
        
        //当前登录用户
        UserInfo user = ConfigHelper.nowUser;
        //密钥内容
        String sigContent;
        sigContent = (user.getSession() == null) ? user.getId() : user.getSession();
//        Log.v("session", sigContent);
        //当前时间
        String time = Utils.time();
        params.add(new BasicNameValuePair("uid", user.getId()));
        params.add(new BasicNameValuePair("time", time));
        params.add(new BasicNameValuePair("sig", Helper.getSig(sigContent, time)));
        
        return params;
    }
    
    /**
     * @deprecated
     * 获取服务器数据
     * @param url
     * @param params
     * @return
     */
    public String responseData(HttpResponse resp) {
        String data = "";
        
        if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            try {
                InputStream is = resp.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                try {
                    StringBuffer sb = new StringBuffer("");
                    char[] buf = new char[512];
                    int l;
                    while ((l = reader.read(buf)) > -1)
                        sb.append(buf, 0, l);
                    data = sb.toString();
                } finally {
                    reader.close();
                    is.close();
                }
            } catch (IOException e) {
                //e.printStackTrace();
                Log.v("OAuth", e.getMessage());
            }
        } else {
            try {
                Log.w("OAuth", "error" + resp.getStatusLine().getStatusCode() + EntityUtils.toString(resp.getEntity()));
            } catch (IOException e) {
                
            }
        }
        Log.v("htt_request_data", data);
        return data;
    }
    
    public static List<BasicNameValuePair> createAuthParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        
        //当前登录用户
        UserInfo user = ConfigHelper.nowUser;
        //密钥内容
        String sigContent = (user.getSession() == null) ? user.getId() : user.getSession();
        //当前时间
        String time = Utils.time();
        
        params.add(new BasicNameValuePair("uid", user.getId()));
        params.add(new BasicNameValuePair("time", time));
        params.add(new BasicNameValuePair("sig", Helper.getSig(sigContent, time)));
        
        return params;
    }
}
